import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/common/xindong_all.dart';
import 'package:xindong/manager/network/xindong_networking.dart';
import 'package:xindong/manager/xindong_page_singleton.dart';
import 'package:xindong/models/xindong_banner_class.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/common/xindong_round_image.dart';

class XinDongWoDeBannerView extends StatefulWidget {
  const XinDongWoDeBannerView({super.key});

  @override
  State<XinDongWoDeBannerView> createState() => _XinDongWoDeBannerViewState();
}

class _XinDongWoDeBannerViewState extends State<XinDongWoDeBannerView> {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
      final object = await XinDongNetworking.instance.request(
        "/api/Slide/lists",
        method: XinDongHttpMethod.post,
        parameters: {"tid": 2},
      );
      if (object.code == XinDongServerStatus.success) {
        List list = object.data;
        XinDongAll.instance.slideList = list.map((e) => XinDongBannerClass.fromJson(e)).toList();
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    if (XinDongAll.instance.slideList.isEmpty) {
      return const SizedBox();
    }
    return Padding(
      padding: const EdgeInsets.only(top: 10),
      child: AspectRatio(
        aspectRatio: (screenWidth * 750) / ((screenWidth - 2 * kPaddingValue) * 277),
        child: Swiper(
          itemCount: XinDongAll.instance.slideList.length,
          autoplay: XinDongAll.instance.slideList.length > 1,
          itemBuilder: (BuildContext context, int index) {
            XinDongBannerClass model = XinDongAll.instance.slideList[index];
            return Container(
              padding: const EdgeInsets.only(left: kPaddingValue, right: kPaddingValue),
              child: XinDongRoundImage(url: model.img, radius: 10,),
            );
          },
          onTap: (int index) {
            XinDongBannerClass model = XinDongAll.instance.slideList[index];
            XinDongPageSingleton.instance.jumpToWebYeMian(title: model.title, url: model.url);
          },
        ),
      ),
    );
  }
}
